home *** CD-ROM | disk | FTP | other *** search
/ Game Power / Game Power Vol. 1 (HEPP Computer) (1995).iso / S053 / DISK2 / LEVELS.C < prev    next >
C/C++ Source or Header  |  1994-04-27  |  30KB  |  1,043 lines

  1. /*
  2.    Doom Editor Utility, by Brendon Wyber and Raphaël Quinet.
  3.  
  4.    You are allowed to use any parts of this code in another program, as
  5.    long as you give credits to the authors in the documentation and in
  6.    the program itself.  Read the file README.1ST for more information.
  7.  
  8.    This program comes with absolutely no warranty.
  9.  
  10.    LEVELS.C - Level loading and saving routines.
  11. */
  12.  
  13. /* the includes */
  14. #include "deu.h"
  15. #include "wstructs.h"
  16. #include "things.h"
  17.  
  18. /* external function from objects.c */
  19. extern NPtr CreateNodes( SEPtr); /* SWAP - needs Vertexes */
  20.  
  21. /* the global data */
  22. MDirPtr Level = NULL;        /* master dictionary entry for the level */
  23. int NumThings = 0;        /* number of things */
  24. TPtr Things;            /* things data */
  25. int NumLineDefs = 0;        /* number of line defs */
  26. LDPtr LineDefs;            /* line defs data */
  27. int NumSideDefs = 0;        /* number of side defs */
  28. SDPtr SideDefs;            /* side defs data */
  29. int NumVertexes = 0;        /* number of vertexes */
  30. VPtr Vertexes;            /* vertex data */
  31. int NumSectors = 0;        /* number of sectors */
  32. SPtr Sectors;            /* sectors data */
  33. int NumSegs = 0;        /* number of segments */
  34. SEPtr Segs = NULL;        /* list of segments */
  35. SEPtr LastSeg = NULL;        /* last segment in the list */
  36. int NumSSectors = 0;        /* number of subsectors */
  37. SSPtr SSectors = NULL;        /* list of subsectors */
  38. SSPtr LastSSector = NULL;    /* last subsector in the list */
  39. int NumNodes = 0;        /* number of Nodes */
  40. NPtr Nodes = NULL;        /* nodes tree */
  41. int NumWTexture = 0;        /* number of wall textures */
  42. char **WTexture;        /* array of wall texture names */
  43. int NumFTexture = 0;        /* number of floor/ceiling textures */
  44. char **FTexture;        /* array of texture names */
  45. int MapMaxX = -32768;        /* maximum X value of map */
  46. int MapMaxY = -32768;        /* maximum Y value of map */
  47. int MapMinX = 32767;        /* minimum X value of map */
  48. int MapMinY = 32767;        /* minimum Y value of map */
  49. Bool MadeChanges = FALSE;    /* made changes? */
  50. Bool MadeMapChanges = FALSE;    /* made changes that need rebuilding? */
  51. SelPtr errld = NULL;        /* LineDefs in error (Nodes builder) */
  52.  
  53.  
  54. /*
  55.    read in the level data
  56. */
  57.  
  58. void ReadLevelData( int episode, int mission) /* SWAP! */
  59. {
  60.    MDirPtr dir;
  61.    char name[ 7];
  62.    int n, m;
  63.    int val;
  64.    int OldNumVertexes;
  65.    int *VertexUsed;
  66.  
  67.    /* No objects are needed: they may be swapped after they have been read */
  68.    ObjectsNeeded( 0);
  69.  
  70.    /* find the various level information from the master directory */
  71.    sprintf( name, "E%dM%d", episode, mission);
  72.    DisplayMessage( -1, -1, "Reading data for level %s...", name);
  73.    Level = FindMasterDir( MasterDir, name);
  74.    if (!Level)
  75.       ProgError( "level data not found");
  76.  
  77.    /* get the number of Vertices */
  78.    dir = FindMasterDir( Level, "VERTEXES");
  79.    OldNumVertexes = (int) (dir->dir.size / 4L);
  80.    if (OldNumVertexes > 0)
  81.    {
  82.       VertexUsed = GetMemory( OldNumVertexes * sizeof( int));
  83.       for (n = 0; n < OldNumVertexes; n++)
  84.      VertexUsed[ n] = FALSE;
  85.    }
  86.  
  87.    /* read in the Things data */
  88.    dir = FindMasterDir( Level, "THINGS");
  89.    NumThings = (int) (dir->dir.size / 10L);
  90.    if (NumThings > 0)
  91.    {
  92.       Things = GetFarMemory( (unsigned long) NumThings * sizeof( struct Thing));
  93.       BasicWadSeek( dir->wadfile, dir->dir.start);
  94.       for (n = 0; n < NumThings; n++)
  95.       {
  96.      BasicWadRead( dir->wadfile, &(Things[ n].xpos), 2);
  97.      BasicWadRead( dir->wadfile, &(Things[ n].ypos), 2);
  98.      BasicWadRead( dir->wadfile, &(Things[ n].angle), 2);
  99.      BasicWadRead( dir->wadfile, &(Things[ n].type), 2);
  100.      BasicWadRead( dir->wadfile, &(Things[ n].when), 2);
  101.       }
  102.    }
  103.  
  104.    /* read in the LineDef information */
  105.    dir = FindMasterDir( Level, "LINEDEFS");
  106.    NumLineDefs = (int) (dir->dir.size / 14L);
  107.    if (NumLineDefs > 0)
  108.    {
  109.       LineDefs = GetFarMemory( (unsigned long) NumLineDefs * sizeof( struct LineDef));
  110.       BasicWadSeek( dir->wadfile, dir->dir.start);
  111.       for (n = 0; n < NumLineDefs; n++)
  112.       {
  113.      BasicWadRead( dir->wadfile, &(LineDefs[ n].start), 2);
  114.      VertexUsed[ LineDefs[ n].start] = TRUE;
  115.      BasicWadRead( dir->wadfile, &(LineDefs[ n].end), 2);
  116.      VertexUsed[ LineDefs[ n].end] = TRUE;
  117.      BasicWadRead( dir->wadfile, &(LineDefs[ n].flags), 2);
  118.      BasicWadRead( dir->wadfile, &(LineDefs[ n].type), 2);
  119.      BasicWadRead( dir->wadfile, &(LineDefs[ n].tag), 2);
  120.      BasicWadRead( dir->wadfile, &(LineDefs[ n].sidedef1), 2);
  121.      BasicWadRead( dir->wadfile, &(LineDefs[ n].sidedef2), 2);
  122.       }
  123.    }
  124.  
  125.    /* read in the SideDef information */
  126.    dir = FindMasterDir( Level, "SIDEDEFS");
  127.    NumSideDefs = (int) (dir->dir.size / 30L);
  128.    if (NumSideDefs > 0)
  129.    {
  130.       SideDefs = GetFarMemory( (unsigned long) NumSideDefs * sizeof( struct SideDef));
  131.       BasicWadSeek( dir->wadfile, dir->dir.start);
  132.       for (n = 0; n < NumSideDefs; n++)
  133.       {
  134.      BasicWadRead( dir->wadfile, &(SideDefs[ n].xoff), 2);
  135.      BasicWadRead( dir->wadfile, &(SideDefs[ n].yoff), 2);
  136.      BasicWadRead( dir->wadfile, &(SideDefs[ n].tex1), 8);
  137.      BasicWadRead( dir->wadfile, &(SideDefs[ n].tex2), 8);
  138.      BasicWadRead( dir->wadfile, &(SideDefs[ n].tex3), 8);
  139.      BasicWadRead( dir->wadfile, &(SideDefs[ n].sector), 2);
  140.       }
  141.    }
  142.  
  143.    /* read in the Vertices which are all the corners of the level, but ignore the */
  144.    /* Vertices not used in any LineDef (they usually are at the end of the list). */
  145.    NumVertexes = 0;
  146.    for (n = 0; n < OldNumVertexes; n++)
  147.       if (VertexUsed[ n])
  148.      NumVertexes++;
  149.    if (NumVertexes > 0)
  150.    {
  151.       Vertexes = GetFarMemory( (unsigned long) NumVertexes * sizeof( struct Vertex));
  152.       dir = FindMasterDir( Level, "VERTEXES");
  153.       BasicWadSeek( dir->wadfile, dir->dir.start);
  154.       MapMaxX = -32768;
  155.       MapMaxY = -32768;
  156.       MapMinX = 32767;
  157.       MapMinY = 32767;
  158.       m = 0;
  159.       for (n = 0; n < OldNumVertexes; n++)
  160.       {
  161.      BasicWadRead( dir->wadfile, &val, 2);
  162.      if (VertexUsed[ n])
  163.      {
  164.         if (val < MapMinX)
  165.            MapMinX = val;
  166.         if (val > MapMaxX)
  167.            MapMaxX = val;
  168.         Vertexes[ m].x = val;
  169.      }
  170.      BasicWadRead( dir->wadfile, &val, 2);
  171.      if (VertexUsed[ n])
  172.      {
  173.         if (val < MapMinY)
  174.            MapMinY = val;
  175.         if (val > MapMaxY)
  176.            MapMaxY = val;
  177.         Vertexes[ m].y = val;
  178.         m++;
  179.      }
  180.       }
  181.       if (m != NumVertexes)
  182.      ProgError("inconsistency in the Vertexes data\n");
  183.    }
  184.  
  185.    if (OldNumVertexes > 0)
  186.    {
  187.       /* update the Vertex numbers in the LineDefs (not really necessary, but...) */
  188.       m = 0;
  189.       for (n = 0; n < OldNumVertexes; n++)
  190.      if (VertexUsed[ n])
  191.         VertexUsed[ n] = m++;
  192.       ObjectsNeeded( OBJ_LINEDEFS, 0);
  193.       for (n = 0; n < NumLineDefs; n++)
  194.       {
  195.      LineDefs[ n].start = VertexUsed[ LineDefs[ n].start];
  196.      LineDefs[ n].end = VertexUsed[ LineDefs[ n].end];
  197.       }
  198.       ObjectsNeeded( 0);
  199.       FreeMemory( VertexUsed);
  200.    }
  201.  
  202.    /* ignore the Segs, SSectors and Nodes */
  203.  
  204.    /* read in the Sectors information */
  205.    dir = FindMasterDir( Level, "SECTORS");
  206.    NumSectors = (int) (dir->dir.size / 26L);
  207.    if (NumSectors > 0)
  208.    {
  209.       Sectors = GetFarMemory( (unsigned long) NumSectors * sizeof( struct Sector));
  210.       BasicWadSeek( dir->wadfile, dir->dir.start);
  211.       for (n = 0; n < NumSectors; n++)
  212.       {
  213.      BasicWadRead( dir->wadfile, &(Sectors[ n].floorh), 2);
  214.      BasicWadRead( dir->wadfile, &(Sectors[ n].ceilh), 2);
  215.      BasicWadRead( dir->wadfile, &(Sectors[ n].floort), 8);
  216.      BasicWadRead( dir->wadfile, &(Sectors[ n].ceilt), 8);
  217.      BasicWadRead( dir->wadfile, &(Sectors[ n].light), 2);
  218.      BasicWadRead( dir->wadfile, &(Sectors[ n].special), 2);
  219.      BasicWadRead( dir->wadfile, &(Sectors[ n].tag), 2);
  220.       }
  221.    }
  222.  
  223.    /* ignore the last entries (Reject & BlockMap) */
  224. }
  225.  
  226.  
  227.  
  228. /*
  229.    forget the level data
  230. */
  231.  
  232. void ForgetLevelData() /* SWAP! */
  233. {
  234.    /* forget the Things */
  235.    ObjectsNeeded( OBJ_THINGS, 0);
  236.    NumThings = 0;
  237.    if (Things)
  238.       FreeFarMemory( Things);
  239.    Things = NULL;
  240.  
  241.    /* forget the Vertices */
  242.    ObjectsNeeded( OBJ_VERTEXES, 0);
  243.    NumVertexes = 0;
  244.    if (Vertexes)
  245.       FreeFarMemory( Vertexes);
  246.    Vertexes = NULL;
  247.  
  248.    /* forget the LineDefs */
  249.    ObjectsNeeded( OBJ_LINEDEFS, 0);
  250.    NumLineDefs = 0;
  251.    if (LineDefs)
  252.       FreeFarMemory( LineDefs);
  253.    LineDefs = NULL;
  254.  
  255.    /* forget the SideDefs */
  256.    ObjectsNeeded( OBJ_SIDEDEFS, 0);
  257.    NumSideDefs = 0;
  258.    if (SideDefs)
  259.       FreeFarMemory( SideDefs);
  260.    SideDefs = NULL;
  261.  
  262.    /* forget the Sectors */
  263.    ObjectsNeeded( OBJ_SECTORS, 0);
  264.    NumSectors = 0;
  265.    if (Sectors)
  266.       FreeFarMemory( Sectors);
  267.    Sectors = NULL;
  268.    ObjectsNeeded( 0);
  269. }
  270.  
  271.  
  272.  
  273. /*
  274.    recursively save the Nodes data to a PWAD file
  275. */
  276.  
  277. void SaveNodes( FILE *file, NPtr node)
  278. {
  279.    /* Nodes tree walk: save child1, save child2, save parent */
  280.    if ((node->child1 & 0x8000) == 0)
  281.    {
  282.       SaveNodes( file, node->node1);
  283.       node->child1 = node->node1->num;
  284.    }
  285.    if ((node->child2 & 0x8000) == 0)
  286.    {
  287.       SaveNodes( file, node->node2);
  288.       node->child2 = node->node2->num;
  289.    }
  290.    WriteBytes( file, &(node->x), 2L);
  291.    WriteBytes( file, &(node->y), 2L);
  292.    WriteBytes( file, &(node->dx), 2L);
  293.    WriteBytes( file, &(node->dy), 2L);
  294.    WriteBytes( file, &(node->maxy1), 2L);
  295.    WriteBytes( file, &(node->miny1), 2L);
  296.    WriteBytes( file, &(node->minx1), 2L);
  297.    WriteBytes( file, &(node->maxx1), 2L);
  298.    WriteBytes( file, &(node->maxy2), 2L);
  299.    WriteBytes( file, &(node->miny2), 2L);
  300.    WriteBytes( file, &(node->minx2), 2L);
  301.    WriteBytes( file, &(node->maxx2), 2L);
  302.    WriteBytes( file, &(node->child1), 2L);
  303.    WriteBytes( file, &(node->child2), 2L);
  304.    node->num = NumNodes++;
  305. }
  306.  
  307.  
  308.  
  309. /*
  310.    forget the Nodes
  311. */
  312.  
  313. void ForgetNodes( NPtr node)
  314. {
  315.    if ((node->child1 & 0x8000) == 0)
  316.       ForgetNodes( node->node1);
  317.    if ((node->child2 & 0x8000) == 0)
  318.       ForgetNodes( node->node2);
  319.    FreeFarMemory( node);
  320. }
  321.  
  322.  
  323.  
  324. /*
  325.    save the level data to a PWAD file
  326. */
  327.  
  328. void SaveLevelData( char *outfile) /* SWAP! */
  329. {
  330.    FILE   *file;
  331.    MDirPtr dir;
  332.    long    counter = 11;
  333.    int     n, i, j;
  334.    void   *data;
  335.    long    size;
  336.    long    dirstart;
  337.    int    *blockptr;
  338.    long    blocksize;
  339.    int     blockcount;
  340.    long    oldpos;
  341.    Bool    newnodes;
  342.    long    rejectsize;
  343.    int     oldNumVertexes;
  344.    SelPtr  cur;
  345.  
  346.    DisplayMessage( -1, -1, "Saving data to \"%s\"...", outfile);
  347.    LogMessage( ": Saving data to \"%s\"...\n", outfile);
  348.    oldNumVertexes = NumVertexes;
  349.    /* open the file */
  350.    if ((file = fopen( outfile, "wb")) == NULL)
  351.       ProgError( "Unable to open file \"%s\"", outfile);
  352.    WriteBytes( file, "PWAD", 4L);     /* PWAD file */
  353.    WriteBytes( file, &counter, 4L);   /* 11 entries */
  354.    WriteBytes( file, &counter, 4L);   /* fix this up later */
  355.    counter = 12L;
  356.    dir = Level->next;
  357.  
  358.    /* output the things data */
  359.    ObjectsNeeded( OBJ_THINGS, 0);
  360.    for (n = 0; n < NumThings; n++)
  361.    {
  362.       WriteBytes( file, &(Things[ n].xpos), 2L);
  363.       WriteBytes( file, &(Things[ n].ypos), 2L);
  364.       WriteBytes( file, &(Things[ n].angle), 2L);
  365.       WriteBytes( file, &(Things[ n].type), 2L);
  366.       WriteBytes( file, &(Things[ n].when), 2L);
  367.       counter += 10L;
  368.    }
  369.    dir = dir->next;
  370.  
  371.    /* update MapMinX, MapMinY, MapMaxX, MapMaxY */
  372.    ObjectsNeeded( OBJ_VERTEXES, 0);
  373.    MapMaxX = -32768;
  374.    MapMaxY = -32768;
  375.    MapMinX = 32767;
  376.    MapMinY = 32767;
  377.    for (n = 0; n < NumVertexes; n++)
  378.    {
  379.       if (Vertexes[ n].x < MapMinX)
  380.      MapMinX = Vertexes[ n].x;
  381.       if (Vertexes[ n].x > MapMaxX)
  382.      MapMaxX = Vertexes[ n].x;
  383.       if (Vertexes[ n].y < MapMinY)
  384.      MapMinY = Vertexes[ n].y;
  385.       if (Vertexes[ n].y > MapMaxY)
  386.      MapMaxY = Vertexes[ n].y;
  387.    }
  388.  
  389.    /* do we need to rebuild the Nodes, Segs and SSectors? */
  390.    if (MadeMapChanges && (Expert || Confirm( -1, 270, "Do you want to rebuild the NODES, SEGS, SSECTORS, REJECT and BLOCKMAP?",
  391.                               "WARNING: You won't be able to use your level if you don't do this...")))
  392.    {
  393.       SEPtr seglist, lastseg;
  394.  
  395.       if (UseMouse)
  396.      HideMousePointer();
  397.       ClearScreen();
  398.       DrawScreenBox3D( 218, 0, ScrMaxX, 55);
  399.       SetColor( WHITE);
  400.       DrawScreenText( 225, 10, "Rebuilding the NODES...");
  401.       DrawScreenBoxHollow( 225, 28, ScrMaxX - 10, 48);
  402.       DrawScreenMeter( 225, 28, ScrMaxX - 10, 48, 0.0);
  403.       if (UseMouse)
  404.      ShowMousePointer();
  405.       seglist = NULL;
  406.       ObjectsNeeded( OBJ_LINEDEFS, OBJ_VERTEXES, 0);
  407.       for (n = 0; n < NumLineDefs; n++)
  408.       {
  409.      if (LineDefs[ n].sidedef1 >= 0)
  410.      {
  411.         if (seglist)
  412.         {
  413.            LastSeg->next = GetMemory( sizeof( struct Seg));
  414.            LastSeg = LastSeg->next;
  415.         }
  416.         else
  417.         {
  418.            seglist = GetMemory( sizeof( struct Seg));
  419.            LastSeg = seglist;
  420.         }
  421.         LastSeg->next = NULL;
  422.         LastSeg->start = LineDefs[ n].start;
  423.         LastSeg->end = LineDefs[ n].end;
  424.         LastSeg->angle = ComputeAngle(Vertexes[ LineDefs[ n].end].x - Vertexes[ LineDefs[ n].start].x,
  425.                       Vertexes[ LineDefs[ n].end].y - Vertexes[ LineDefs[ n].start].y);
  426.         LastSeg->linedef = n;
  427.         LastSeg->flip = 0;
  428.         LastSeg->dist = 0;
  429.      }
  430.      if (LineDefs[ n].sidedef2 >= 0)
  431.      {
  432.         if (seglist)
  433.         {
  434.            LastSeg->next = GetMemory( sizeof( struct Seg));
  435.            LastSeg = LastSeg->next;
  436.         }
  437.         else
  438.         {
  439.            seglist = GetMemory( sizeof( struct Seg));
  440.            LastSeg = seglist;
  441.         }
  442.         LastSeg->next = NULL;
  443.         LastSeg->start = LineDefs[ n].end;
  444.         LastSeg->end = LineDefs[ n].start;
  445.         LastSeg->angle = ComputeAngle(Vertexes[ LineDefs[ n].start].x - Vertexes[ LineDefs[ n].end].x,
  446.                       Vertexes[ LineDefs[ n].start].y - Vertexes[ LineDefs[ n].end].y);
  447.         LastSeg->linedef = n;
  448.         LastSeg->flip = 1;
  449.         LastSeg->dist = 0;
  450.      }
  451.       }
  452.       ShowProgress( OBJ_VERTEXES);
  453.       ShowProgress( OBJ_SIDEDEFS);
  454.       ObjectsNeeded( OBJ_VERTEXES, 0);
  455.       errld = NULL;
  456.       Nodes = CreateNodes( seglist);
  457.       if (UseMouse)
  458.      HideMousePointer();
  459.       DrawScreenMeter( 225, 28, ScrMaxX - 10, 48, 1.0);
  460.       if (errld != NULL)
  461.       {
  462.      DrawScreenBox3D( 0, 130, 203, ScrMaxY);
  463.      DrawScreenText( 10, 138, "Suspicious LineDefs:");
  464.      n = 153;
  465.      for (cur = errld; cur && n < ScrMaxY - 12; cur = cur->next)
  466.      {
  467.         DrawScreenText( 40, n, "#%d", cur->objnum);
  468.         n += 10;
  469.      }
  470.       }
  471.       if (UseMouse)
  472.      ShowMousePointer();
  473.       newnodes = TRUE;
  474.    }
  475.    else
  476.       newnodes = FALSE;
  477.  
  478.    /* output the LineDefs */
  479.    ObjectsNeeded( OBJ_LINEDEFS, 0);
  480.    for (n = 0; n < NumLineDefs; n++)
  481.    {
  482.       WriteBytes( file, &(LineDefs[ n].start), 2L);
  483.       WriteBytes( file, &(LineDefs[ n].end), 2L);
  484.       WriteBytes( file, &(LineDefs[ n].flags), 2L);
  485.       WriteBytes( file, &(LineDefs[ n].type), 2L);
  486.       WriteBytes( file, &(LineDefs[ n].tag), 2L);
  487.       WriteBytes( file, &(LineDefs[ n].sidedef1), 2L);
  488.       WriteBytes( file, &(LineDefs[ n].sidedef2), 2L);
  489.       counter += 14L;
  490.    }
  491.    dir = dir->next;
  492.  
  493.    /* output the SideDefs */
  494.    ObjectsNeeded( OBJ_SIDEDEFS, 0);
  495.    for (n = 0; n < NumSideDefs; n++)
  496.    {
  497.       WriteBytes( file, &(SideDefs[ n].xoff), 2L);
  498.       WriteBytes( file, &(SideDefs[ n].yoff), 2L);
  499.       WriteBytes( file, &(SideDefs[ n].tex1), 8L);
  500.       WriteBytes( file, &(SideDefs[ n].tex2), 8L);
  501.       WriteBytes( file, &(SideDefs[ n].tex3), 8L);
  502.       WriteBytes( file, &(SideDefs[ n].sector), 2L);
  503.       counter += 30L;
  504.    }
  505.    dir = dir->next;
  506.  
  507.    if (MadeMapChanges)
  508.    {
  509.       /* output the Vertices */
  510.       ObjectsNeeded( OBJ_VERTEXES, 0);
  511.       for (n = 0; n < NumVertexes; n++)
  512.       {
  513.      WriteBytes( file, &(Vertexes[ n].x), 2L);
  514.      WriteBytes( file, &(Vertexes[ n].y), 2L);
  515.      counter += 4L;
  516.       }
  517.    }
  518.    else
  519.    {
  520.       /* copy the Vertices */
  521.       ObjectsNeeded( 0);
  522.       size = dir->dir.size;
  523.       counter += size;
  524.       BasicWadSeek( dir->wadfile, dir->dir.start);
  525.       CopyBytes( file, dir->wadfile->fileinfo, size);
  526.    }
  527.    dir = dir->next;
  528.  
  529.    if (newnodes)
  530.    {
  531.       SEPtr curse, oldse;
  532.       SSPtr curss, oldss;
  533.  
  534.       ObjectsNeeded( 0);
  535.       /* output and forget the Segments */
  536.       curse = Segs;
  537.       while (curse)
  538.       {
  539.      WriteBytes( file, &(curse->start), 2L);
  540.      WriteBytes( file, &(curse->end), 2L);
  541.      WriteBytes( file, &(curse->angle), 2L);
  542.      WriteBytes( file, &(curse->linedef), 2L);
  543.      WriteBytes( file, &(curse->flip), 2L);
  544.      WriteBytes( file, &(curse->dist), 2L);
  545.      oldse = curse;
  546.      curse = curse->next;
  547.      FreeFarMemory( oldse);
  548.      counter += 12L;
  549.       }
  550.       Segs = NULL;
  551.       dir = dir->next;
  552.  
  553.       /* output and forget the SSectors */
  554.       curss = SSectors;
  555.       while (curss)
  556.       {
  557.      WriteBytes( file, &(curss->num), 2L);
  558.      WriteBytes( file, &(curss->first), 2L);
  559.      oldss = curss;
  560.      curss = curss->next;
  561.      FreeFarMemory( oldss);
  562.      counter += 4L;
  563.       }
  564.       SSectors = NULL;
  565.       dir = dir->next;
  566.  
  567.       /* output the Nodes */
  568.       NumNodes = 0;
  569.       SaveNodes( file, Nodes);
  570.       counter += (long) NumNodes * 28L;
  571.       dir = dir->next;
  572.  
  573.       /* forget the Nodes */
  574.       ForgetNodes( Nodes);
  575.       Nodes = NULL;
  576.    }
  577.    else
  578.    {
  579.       /* copy the Segs, SSectors and Nodes */
  580.       for (n = 0; n < 3; n++)
  581.       {
  582.      size = dir->dir.size;
  583.      counter += size;
  584.      BasicWadSeek( dir->wadfile, dir->dir.start);
  585.      CopyBytes( file, dir->wadfile->fileinfo, size);
  586.      dir = dir->next;
  587.       }
  588.    }
  589.  
  590.    /* output the Sectors */
  591.    ObjectsNeeded( OBJ_SECTORS, 0);
  592.    for (n = 0; n < NumSectors; n++)
  593.    {
  594.       WriteBytes( file, &(Sectors[ n].floorh), 2L);
  595.       WriteBytes( file, &(Sectors[ n].ceilh), 2L);
  596.       WriteBytes( file, &(Sectors[ n].floort), 8L);
  597.       WriteBytes( file, &(Sectors[ n].ceilt), 8L);
  598.       WriteBytes( file, &(Sectors[ n].light), 2L);
  599.       WriteBytes( file, &(Sectors[ n].special), 2L);
  600.       WriteBytes( file, &(Sectors[ n].tag), 2L);
  601.       counter += 26L;
  602.    }
  603.    dir = dir->next;
  604.  
  605.    if (newnodes)
  606.    {
  607.       /* create and output the reject data */
  608.       ObjectsNeeded( OBJ_SECTORS, 0); /* !!! */
  609.       if (UseMouse)
  610.      HideMousePointer();
  611.       DrawScreenBox3D( 218, 80, ScrMaxX, 135);
  612.       SetColor( WHITE);
  613.       DrawScreenText( 225, 90, "Rebuilding the REJECT data...");
  614.       DrawScreenBoxHollow( 225, 108, ScrMaxX - 10, 128);
  615.       DrawScreenMeter( 225, 108, ScrMaxX - 10, 128, 0.0);
  616.       if (UseMouse)
  617.      ShowMousePointer();
  618.       rejectsize = ((long) NumSectors * (long) NumSectors + 7L) / 8L;
  619.       data = GetMemory( (size_t) rejectsize);
  620.       for (i = 0; i < rejectsize; i++)
  621.      ((char *) data)[ i] = 0;
  622.       for (i = 0; i < NumSectors; i++)
  623.       {
  624.      if (UseMouse)
  625.         HideMousePointer();
  626.      DrawScreenMeter( 225, 108, ScrMaxX - 10, 128, (float) i / (float) NumSectors);
  627.      if (UseMouse)
  628.         ShowMousePointer();
  629.      for (j = 0; j < NumSectors; j++)
  630.      {
  631. /*
  632.         if (Reject( i, j))
  633.            data[ (i * NumSectors + j) / 8] |= 1 <<
  634. */
  635.      }
  636.       }
  637.       if (UseMouse)
  638.      HideMousePointer();
  639.       DrawScreenMeter( 225, 108, ScrMaxX - 10, 128, 1.0);
  640.       if (UseMouse)
  641.      ShowMousePointer();
  642.       WriteBytes( file, data, rejectsize);
  643.       counter += rejectsize;
  644.       dir = dir->next;
  645.       FreeMemory( data);
  646.    }
  647.    else
  648.    {
  649.       /* copy the Reject data */
  650.       ObjectsNeeded( 0);
  651.       rejectsize = dir->dir.size;
  652.       size = rejectsize;
  653.       counter += size;
  654.       BasicWadSeek( dir->wadfile, dir->dir.start);
  655.       CopyBytes( file, dir->wadfile->fileinfo, size);
  656.       dir = dir->next;
  657.    }
  658.  
  659.    if (newnodes)
  660.    {
  661.       /* create and output the blockmap */
  662.       ObjectsNeeded( OBJ_LINEDEFS, OBJ_VERTEXES, 0);
  663.       if (UseMouse)
  664.      HideMousePointer();
  665.       DrawScreenBox3D( 218, 160, ScrMaxX, 215);
  666.       SetColor( WHITE);
  667.       DrawScreenText( 225, 170, "Rebuilding the BLOCKMAP...");
  668.       DrawScreenBoxHollow( 225, 188, ScrMaxX - 10, 208);
  669.       DrawScreenMeter( 225, 188, ScrMaxX - 10, 208, 0.0);
  670.       if (UseMouse)
  671.      ShowMousePointer();
  672.       MapMinX = (int) (MapMinX / 8 - 8) * 8;
  673.       WriteBytes( file, &MapMinX, 2L);
  674.       MapMinY = (int) (MapMinY / 8 - 8) * 8;
  675.       WriteBytes( file, &MapMinY, 2L);
  676.       MapMaxX = MapMaxX / 128 - MapMinX / 128 + 2;
  677.       WriteBytes( file, &MapMaxX, 2L);
  678.       MapMaxY = MapMaxY / 128 - MapMinY / 128 + 2;
  679.       WriteBytes( file, &MapMaxY, 2L);
  680.       counter += 8L;
  681.       oldpos = ftell( file);
  682.       blocksize = (long) (MapMaxX * MapMaxY * sizeof( int));
  683.       blockptr = GetMemory( blocksize);
  684.       WriteBytes( file, blockptr, blocksize);
  685.       blocksize += 8L;
  686.       counter += blocksize - 7L;
  687.       blockcount = MapMaxX * MapMaxY + 4;
  688.       for (i = 0; i < MapMaxY; i++)
  689.       {
  690.      if (UseMouse)
  691.         HideMousePointer();
  692.      DrawScreenMeter( 225, 188, ScrMaxX - 10, 208, (float) i / (float) MapMaxY);
  693.      if (UseMouse)
  694.         ShowMousePointer();
  695.      for (j = 0; j < MapMaxX; j++)
  696.      {
  697.         blockptr[ MapMaxX * i + j] = blockcount;
  698.         n = 0;
  699.         WriteBytes( file, &n, 2L);
  700.         counter += 2L;
  701.         blocksize += 2L;
  702.         blockcount++;
  703.         for (n = 0; n < NumLineDefs; n++)
  704.            if (IsLineDefInside( n, MapMinX + j * 128, MapMinY + i * 128, MapMinX + 127 + j * 128, MapMinY + 127 + i * 128))
  705.            {
  706.           WriteBytes( file, &n, 2L);
  707.           counter += 2L;
  708.           blocksize += 2L;
  709.           blockcount++;
  710.            }
  711.         n = -1;
  712.         WriteBytes( file, &n, 2L);
  713.         counter += 2L;
  714.         blocksize += 2L;
  715.         blockcount++;
  716.      }
  717.       }
  718.       if (UseMouse)
  719.      HideMousePointer();
  720.       DrawScreenMeter( 225, 188, ScrMaxX - 10, 208, 1.0);
  721.       if (UseMouse)
  722.      ShowMousePointer();
  723.       size = ftell( file);
  724.       fseek( file, oldpos, SEEK_SET);
  725.       WriteBytes( file, blockptr, (long) (MapMaxX * MapMaxY * sizeof( int)));
  726.       fseek( file, size, SEEK_SET);
  727.       if (FindMasterDir( dir, "P2_END"))
  728.      counter--;
  729.       FreeMemory( blockptr);
  730.    }
  731.    else
  732.    {
  733.       /* copy the blockmap data */
  734.       ObjectsNeeded( 0);
  735.       blocksize = dir->dir.size;
  736.       size = blocksize;
  737.       counter += size;
  738.       BasicWadSeek( dir->wadfile, dir->dir.start);
  739.       CopyBytes( file, dir->wadfile->fileinfo, size);
  740.       dir = dir->next;
  741.    }
  742.  
  743.  
  744.    /* output the actual directory */
  745.    dirstart = counter;
  746.    counter = 12L;
  747.    size = 0L;
  748.    dir = Level;
  749.    WriteBytes( file, &counter, 4L);
  750.    WriteBytes( file, &size, 4L);
  751.    WriteBytes( file, &(dir->dir.name), 8L);
  752.    dir = dir->next;
  753.  
  754.    size = (long) NumThings * 10L;
  755.    WriteBytes( file, &counter, 4L);
  756.    WriteBytes( file, &size, 4L);
  757.    WriteBytes( file, "THINGS\0\0", 8L);
  758.    counter += size;
  759.    dir = dir->next;
  760.  
  761.    size = (long) NumLineDefs * 14L;
  762.    WriteBytes( file, &counter, 4L);
  763.    WriteBytes( file, &size, 4L);
  764.    WriteBytes( file, "LINEDEFS", 8L);
  765.    counter += size;
  766.    dir = dir->next;
  767.  
  768.    size = (long) NumSideDefs * 30L;
  769.    WriteBytes( file, &counter, 4L);
  770.    WriteBytes( file, &size, 4L);
  771.    WriteBytes( file, "SIDEDEFS", 8L);
  772.    counter += size;
  773.    dir = dir->next;
  774.  
  775.    if (MadeMapChanges)
  776.       size = (long) NumVertexes * 4L;
  777.    else
  778.       size = dir->dir.size;
  779.    WriteBytes( file, &counter, 4L);
  780.    WriteBytes( file, &size, 4L);
  781.    WriteBytes( file, "VERTEXES", 8L);
  782.    counter += size;
  783.    dir = dir->next;
  784.  
  785.    if (newnodes)
  786.       size = (long) NumSegs * 12L;
  787.    else
  788.       size = dir->dir.size;
  789.    WriteBytes( file, &counter, 4L);
  790.    WriteBytes( file, &size, 4L);
  791.    WriteBytes( file, "SEGS\0\0\0\0", 8L);
  792.    counter += size;
  793.    dir = dir->next;
  794.  
  795.    if (newnodes)
  796.       size = (long) NumSSectors * 4L;
  797.    else
  798.       size = dir->dir.size;
  799.    WriteBytes( file, &counter, 4L);
  800.    WriteBytes( file, &size, 4L);
  801.    WriteBytes( file, "SSECTORS", 8L);
  802.    counter += size;
  803.    dir = dir->next;
  804.  
  805.    if (newnodes)
  806.       size = (long) NumNodes * 28L;
  807.    else
  808.       size = dir->dir.size;
  809.    WriteBytes( file, &counter, 4L);
  810.    WriteBytes( file, &size, 4L);
  811.    WriteBytes( file, "NODES\0\0\0", 8L);
  812.    counter += size;
  813.    dir = dir->next;
  814.  
  815.    size = (long) NumSectors * 26L;
  816.    WriteBytes( file, &counter, 4L);
  817.    WriteBytes( file, &size, 4L);
  818.    WriteBytes( file, "SECTORS\0", 8L);
  819.    counter += size;
  820.    dir = dir->next;
  821.  
  822.    size = rejectsize;
  823.    WriteBytes( file, &counter, 4L);
  824.    WriteBytes( file, &size, 4L);
  825.    WriteBytes( file, "REJECT\0\0", 8L);
  826.    counter += size;
  827.    dir = dir->next;
  828.  
  829.    size = blocksize;
  830.    WriteBytes( file, &counter, 4L);
  831.    WriteBytes( file, &size, 4L);
  832.    WriteBytes( file, "BLOCKMAP", 8L);
  833.    counter += size;
  834.    dir = dir->next;
  835.  
  836.    /* fix up the directory start information */
  837.    if (fseek( file, 8L, SEEK_SET))
  838.       ProgError( "error writing to file");
  839.    WriteBytes( file, &dirstart, 4L);
  840.  
  841.    /* close the file */
  842.    fclose( file);
  843.  
  844.    NumSegs = 0;
  845.    NumSSectors = 0;
  846.    NumNodes = 0;
  847.  
  848.    /* delete the vertices added by the Nodes builder */
  849.    if (NumVertexes != oldNumVertexes)
  850.    {
  851.       ObjectsNeeded( OBJ_VERTEXES, 0);
  852.       NumVertexes = oldNumVertexes;
  853.       ResizeFarMemory( Vertexes, NumVertexes * sizeof( struct Vertex));
  854.    }
  855.  
  856.    /* the file is now up to date */
  857.    MadeChanges = FALSE;
  858.    if (newnodes)
  859.    {
  860.       MadeMapChanges = FALSE;
  861.       if (errld != NULL)
  862.       {
  863.      DrawScreenBox3D( 218, ScrMaxY - 30, ScrMaxX, ScrMaxY);
  864.      SetColor( YELLOW);
  865.      DrawScreenText( 225, ScrMaxY - 18, "Errors found.  Press any key to continue...");
  866.      bioskey( 0);
  867.      ForgetSelection( &errld);
  868.       }
  869.    }
  870.    ObjectsNeeded( 0);
  871.  
  872.    /* update pointers in Master Directory */
  873.    OpenPatchWad( outfile);
  874.  
  875.    /* this should free the old "*.BAK" file */
  876.    CloseUnusedWadFiles();
  877.  
  878. }
  879.  
  880.  
  881.  
  882. /*
  883.    function used by qsort to sort the texture names
  884. */
  885. int SortTextures( const void *a, const void *b)
  886. {
  887.    return strcmp( *((char **)a), *((char **)b));
  888. }
  889.  
  890.  
  891.  
  892. /*
  893.    read in the wall texture names
  894. */
  895.  
  896. void ReadWTextureNames()
  897. {
  898.    MDirPtr dir;
  899.    long *offsets;
  900.    int n;
  901.    long val;
  902.  
  903.    printf("Reading wall texture names\n");
  904.    dir = FindMasterDir( MasterDir, "TEXTURE1");
  905.    BasicWadSeek( dir->wadfile, dir->dir.start);
  906.    BasicWadRead( dir->wadfile, &val, 4);
  907.    NumWTexture = val + 1;
  908.    /* read in the offsets for texture1 names */
  909.    offsets = GetMemory( NumWTexture * sizeof( long));
  910.    for (n = 1; n < NumWTexture; n++)
  911.       BasicWadRead( dir->wadfile, &(offsets[ n]), 4);
  912.    /* read in the actual names */
  913.    WTexture = GetMemory( NumWTexture * sizeof( char *));
  914.    WTexture[ 0] = GetMemory( 9 * sizeof( char));
  915.    strcpy(WTexture[ 0], "-");
  916.    for (n = 1; n < NumWTexture; n++)
  917.    {
  918.       WTexture[ n] = GetMemory( 9 * sizeof( char));
  919.       BasicWadSeek( dir->wadfile, dir->dir.start + offsets[ n]);
  920.       BasicWadRead( dir->wadfile, WTexture[ n], 8);
  921.       WTexture[ n][ 8] = '\0';
  922.    }
  923.    FreeMemory( offsets);
  924.    if (Registered)
  925.    {
  926.       dir = FindMasterDir( MasterDir, "TEXTURE2");
  927.       BasicWadSeek( dir->wadfile, dir->dir.start);
  928.       BasicWadRead( dir->wadfile, &val, 4);
  929.       /* read in the offsets for texture2 names */
  930.       offsets = GetMemory( val * sizeof( long));
  931.       for (n = 0; n < val; n++)
  932.      BasicWadRead( dir->wadfile, &(offsets[ n]), 4);
  933.       /* read in the actual names */
  934.       WTexture = ResizeMemory( WTexture, (NumWTexture + val) * sizeof( char *));
  935.       for (n = 0; n < val; n++)
  936.       {
  937.      WTexture[ NumWTexture + n] = GetMemory( 9 * sizeof( char));
  938.      BasicWadSeek( dir->wadfile, dir->dir.start + offsets[ n]);
  939.      BasicWadRead( dir->wadfile, WTexture[ NumWTexture + n], 8);
  940.      WTexture[ NumWTexture + n][ 8] = '\0';
  941.       }
  942.       NumWTexture += val;
  943.       FreeMemory( offsets);
  944.    }
  945.    /* sort the names */
  946.    qsort( WTexture, NumWTexture, sizeof( char *), SortTextures);
  947. }
  948.  
  949.  
  950.  
  951. /*
  952.    forget the wall texture names
  953. */
  954.  
  955. void ForgetWTextureNames()
  956. {
  957.    int n;
  958.  
  959.    /* forget all names */
  960.    for (n = 0; n < NumWTexture; n++)
  961.       FreeMemory( WTexture[ n]);
  962.  
  963.    /* forget the array */
  964.    NumWTexture = 0;
  965.    FreeMemory( WTexture);
  966. }
  967.  
  968.  
  969.  
  970. /*
  971.    read in the floor/ceiling texture names
  972. */
  973.  
  974. void ReadFTextureNames()
  975. {
  976.    MDirPtr dir;
  977.    int n, m;
  978.  
  979.    printf("Reading floor/ceiling texture names\n");
  980.    /* count the names */
  981.    dir = FindMasterDir( MasterDir, "F1_START");
  982.    dir = dir->next;
  983.    for (n = 0; dir && strcmp(dir->dir.name, "F1_END"); n++)
  984.       dir = dir->next;
  985.    NumFTexture = n;
  986.    /* get the actual names from master dir. */
  987.    dir = FindMasterDir( MasterDir, "F1_START");
  988.    dir = dir->next;
  989.    FTexture = GetMemory( NumFTexture * sizeof( char *));
  990.    for (n = 0; n < NumFTexture; n++)
  991.    {
  992.       FTexture[ n] = GetMemory( 9 * sizeof( char));
  993.       strncpy( FTexture[ n], dir->dir.name, 8);
  994.       FTexture[ n][ 8] = '\0';
  995.       dir = dir->next;
  996.    }
  997.    if (Registered)
  998.    {
  999.       /* count the names */
  1000.       dir = FindMasterDir( MasterDir, "F2_START");
  1001.       dir = dir->next;
  1002.       for (n = 0; dir && strcmp(dir->dir.name, "F2_END"); n++)
  1003.      dir = dir->next;
  1004.       /* get the actual names from master dir. */
  1005.       dir = FindMasterDir( MasterDir, "F2_START");
  1006.       dir = dir->next;
  1007.       FTexture = ResizeMemory( FTexture, (NumFTexture + n) * sizeof( char *));
  1008.       for (m = NumFTexture; m < NumFTexture + n; m++)
  1009.       {
  1010.      FTexture[ m] = GetMemory( 9 * sizeof( char));
  1011.      strncpy( FTexture[ m], dir->dir.name, 8);
  1012.      FTexture[ m][ 8] = '\0';
  1013.      dir = dir->next;
  1014.       }
  1015.       NumFTexture += n;
  1016.    }
  1017.    /* sort the names */
  1018.    qsort( FTexture, NumFTexture, sizeof( char *), SortTextures);
  1019. }
  1020.  
  1021.  
  1022.  
  1023. /*
  1024.    forget the floor/ceiling texture names
  1025. */
  1026.  
  1027. void ForgetFTextureNames()
  1028. {
  1029.    int n;
  1030.  
  1031.    /* forget all names */
  1032.    for (n = 0; n < NumFTexture; n++)
  1033.       FreeMemory( FTexture[ n]);
  1034.  
  1035.    /* forget the array */
  1036.    NumFTexture = 0;
  1037.    FreeMemory( FTexture);
  1038. }
  1039.  
  1040.  
  1041.  
  1042. /* end of file */
  1043.